package com.base.app.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.app.dao.ArticleDao;
import com.base.app.dao.ArticlePictureDao;
import com.base.app.entity.Article;
import com.base.data.exception.DataException;
import com.mysteel.page.Page;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService
{

	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ArticlePictureDao articlePictureDao;
	@Autowired
	private ArticlePictureService articlePictureService;
	
	@Override
	public void saveArticle(Article article)
	{
		Assert.notNull (article);
		
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("title" ,article.getTitle ());
		List<Long> page = articleDao.query(params);
		
		if(ObjectUtils.notEmpty (page))
		{
			throw new DataException("该文章已经存在!");
		}
		
		articleDao.save (article);
	}

	@Override
	public void deleteArticleById(long id,HttpServletRequest request)
	{	
		
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("articleId" ,id);
		List<Long> list = articlePictureDao.query (params);
		
		if(ObjectUtils.notEmpty (list))
		{
			for(long ids:list)
			{
				articlePictureService.deleteArticlePicture  (ids,request);
			}
		}
		articleDao.delete (id);
	}

	@Override
	public void updateArticle(Article article)
	{
		Assert.notNull (article);
		
		Article ar = articleDao.get (article.getId ());
		
		if(ObjectUtils.notNull (ar))
		{
			ar.setChannelId (article.getChannelId ());
			ar.setTitle (article.getTitle ());
			ar.setContent (article.getContent ());
			ar.setSubTitle (article.getSubTitle ());
			ar.setKeyword (article.getKeyword ());
			ar.setSummary (article.getSummary ());
			ar.setAuthor (article.getAuthor ());
			ar.setSource (article.getSource ());
			ar.setStatus (article.isStatus ());
			ar.setLevel (article.getLevel ());
			ar.setAdminId (article.getAdminId ());
			ar.setAdminName(article.getAdminName ());
			ar.setLastAccess (article.getLastAccess ());
			ar.setLastAccessEditor (article.getLastAccessEditor ());
			
			articleDao.update (article);
		}
	}

	@Override
	public Article getArticleById(long id)
	{
		
		return articleDao.get (id);
	}

	@Override
	public Page<Article> queryArticle(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		
		Page<Article> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = articleDao.count(params);

		if (count > 0)
		{
			page = new Page<Article>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> ars = articleDao.query(params);

			if (ObjectUtils.notEmpty(ars))
			{
				List<Article> articles = new ArrayList<Article>(ars.size());

				for (long arId : ars)
				{
					articles.add(getArticleById (arId));
				}

				page.setDatas(articles);
			}
		}

		return page;
	}
	
}
